package grame

/**
 * @author Shining Wang <shiningwang@topca.com>
 */
@SuppressWarnings("GroovyUnusedDeclaration")
class User {
    String id = UUID.randomUUID().toString()
    String username
    String passwordHash
    Date dateCreated
    Date lastUpdated

    static hasMany = [roles: Role, permissions: String]

    static constraints = {
        id shared: 'id', bindable: true
        username shared: 'name', nullable: false, blank: false
    }

    /* 用户默认具有Anyone角色 */

    def beforeInsert() {
        if (!roles || !roles.contains(Role.findByName(SystemRoleName.Anyone.name())))
            addToRoles(Role.findByName(SystemRoleName.Anyone.name()));
    }

    @Override
    public String toString() {
        return "${domainClass.name} : ${username}(${id})"
    }

    public User setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash
        return this
    }
}
